﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Windows.Forms;
using Controls;
using System.Reflection;
using System.Data;

namespace UIMarker
{
    public class UIHelper
    {
        /// <summary>
        /// 设置控件状态
        /// </summary>
        public static void SetControlState(string actionMode, System.Windows.Forms.Control.ControlCollection ctls)
        {

            if (null == ctls || 0 == ctls.Count) return;

            foreach (Control c in ctls)
            {
                if (c is PsTextBox)
                {
                    PsTextBox txt = c as PsTextBox;
                    if (txt.SysReadOnly) { txt.ReadOnly = true; continue; }
                    txt.Enabled = (null != txt.EditMode && (txt.EditMode.IndexOf(actionMode) > -1));
                }
                else if (c is PsPopWin)
                {
                    PsPopWin pop = c as PsPopWin;
                    if (pop.SysReadOnly) { pop.Enabled = false; continue; }
                    pop.Enabled = (null != pop.EditMode && (pop.EditMode.IndexOf(actionMode) > -1));
                }
                else if (c is PsDatePicker)
                {
                    PsDatePicker pdp = c as PsDatePicker;
                    if (pdp.SysReadOnly) { pdp.Enabled = false; continue; }
                    pdp.Enabled = (null != pdp.EditMode && (pdp.EditMode.IndexOf(actionMode) > -1));
                }
                else if (c is PsSelect)
                {
                    PsSelect sls = c as PsSelect;
                    if (sls.SysReadOnly) { sls.Enabled = false; continue; }
                    sls.Enabled = (null != sls.EditMode && (sls.EditMode.IndexOf(actionMode) > -1));
                }
            }
        }

        /// <summary>
        /// 根据控件值，自动填充实体
        /// </summary>
        /// <param name="o"></param>
        /// <param name="ctls"></param>
        public static void FillModelFromUI(object o, System.Windows.Forms.Control.ControlCollection ctls)
        {
            Type oType = o.GetType();
            PropertyInfo[] propertys = oType.GetProperties();

            string name = "";
            Control c = null;
            string tv = null;
            foreach (PropertyInfo info in propertys)
            {
                name = info.Name.ToLower();
                if (!ctls.ContainsKey(name)) continue;

                c = ctls[name];
                tv = UIHelper.GetValue(c); //获得控件的值

                UIHelper.AddValue(info, tv, o);
            }
        }

        /// <summary>
        /// 从GridViewRow中得到数据填充实体
        /// </summary>
        /// <param name="o"></param>
        /// <param name="dr"></param>
        public static void FillModelFromDataGridViewRow(object o, DataGridViewRow dr)
        {
            Type oType = o.GetType();
            PropertyInfo[] propertys = oType.GetProperties();

            string name = "";
            object tv = null;
            DataGridView dg = dr.DataGridView;
            DataGridViewColumnCollection cc = dg.Columns;
            foreach (PropertyInfo info in propertys)
            {
                name = info.Name.ToLower();
                if (!cc.Contains(name)) continue;

                tv = dr.Cells[name].Value; //获得控件的值

                UIHelper.AddValue(info, (null == tv ? "" : tv.ToString()), o);
            }
        }

        /// <summary>
        /// 得到控件的值，本例子中只有两种控件，故先这样写
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string GetValue(Control c)
        {
            if (null == c) return null;

            if (c is PsPopWin)
            {
                PsPopWin pop = c as PsPopWin;
                //弹出框如果设置了Value，则返回Value，否则返回Text
                return (null == pop) ? null : (string.IsNullOrEmpty(pop.Value) ? pop.Text : pop.Value);
            }
            else if (c is TextBox)
            {
                TextBox txt = c as TextBox;
                return (null == txt) ? null : txt.Text;
            }
            else if (c is DateTimePicker)
            {
                DateTimePicker dp = c as DateTimePicker;
                if (null == dp) return null;
                if (dp.ShowCheckBox && !dp.Checked) return null; //
                return dp.Value.ToShortDateString();
            }
            else if (c is PsSelect)
            {
                PsSelect sls = c as PsSelect;
                return (null == sls) ? null : sls.Text;
            }
            return null;
        }

        public static void FillUIFromDataTable(DataTable dt, System.Windows.Forms.Control.ControlCollection ctls)
        {
            if (dt == null || dt.Rows.Count == 0) return;
            DataRow dr = dt.Rows[0];
            string col = "";
            Control c = null;
            string v = "";
            object t = null; //兼容弹出窗口

            DataColumnCollection dcc = dt.Columns;
            foreach (DataColumn dc in dt.Columns)
            {
                col = dc.ColumnName.ToLower();

                if (!ctls.ContainsKey(col) || !dcc.Contains(col)) continue;
                c = ctls[col];
                if (null == c || null == dr[col]) continue;

                v = dr[col].ToString();
                t = dcc.Contains(col + "_desc") ? dr[col + "_desc"] : null;

                UIHelper.FillControl(c, v, t);
            }
        }

        public static void FillDataGridViewRowFromDataRow(DataRow dr, DataGridViewRow dgr)
        {
            if (dr == null) return;
            string col = "";

            DataColumnCollection dcc = dr.Table.Columns;
            DataGridViewColumnCollection dvcc = dgr.DataGridView.Columns;
            foreach (DataColumn dc in dcc)
            {
                col = dc.ColumnName.ToLower();

                if (!dvcc.Contains(col)) continue;
                dgr.Cells[col].Value = dr[col];
            }
        }

        public static void FillUIFromModel(object o, System.Windows.Forms.Control.ControlCollection ctls)
        {
            Type oType = o.GetType();
            PropertyInfo[] propertys = oType.GetProperties();

            string name = "";
            Control c = null;
            object v = null;
            foreach (PropertyInfo info in propertys)
            {
                name = info.Name.ToLower();
                if (!ctls.ContainsKey(name)) continue;

                c = ctls[name];
                v = info.GetValue(o, null);
                UIHelper.FillControl(c, null == v ? "" : v.ToString(), null);
            }

        }

        /// <summary>
        /// 向属性中添加对应类型的值
        /// </summary>
        /// <param name="info">属性对象</param>
        /// <param name="Value">值</param>
        /// <param name="obj">属性所属对象</param>
        /// <param name="hasKey">Request对象中是否有该属性</param>
        private static void AddValue(PropertyInfo info, String value, Object obj)
        {
            if (string.IsNullOrEmpty(value)) //如果是空
            {
                info.SetValue(obj, null, null);
                return;
            }
            if (info.PropertyType == typeof(String))
            {
                info.SetValue(obj, value, null);
            }
            else if (info.PropertyType == typeof(int?) || info.PropertyType == typeof(int))
            {
                info.SetValue(obj, int.Parse(value), null);
            }
            else if (info.PropertyType == typeof(DateTime?) || info.PropertyType == typeof(DateTime))
            {
                info.SetValue(obj, DateTime.Parse(value), null);
            }
            else if (info.PropertyType == typeof(Decimal?) || info.PropertyType == typeof(Decimal))
            {
                info.SetValue(obj, Decimal.Parse(value), null);
            }
            else if (info.PropertyType == typeof(Double?) || info.PropertyType == typeof(Double))
            {
                info.SetValue(obj, Double.Parse(value), null);
            }
            else if (info.PropertyType == typeof(float?) || info.PropertyType == typeof(float))
            {
                info.SetValue(obj, float.Parse(value), null);
            }
            else if (info.PropertyType == typeof(bool?) || info.PropertyType == typeof(bool))
            {
                info.SetValue(obj, (value != "0"), null);
            }
            else if (info.PropertyType == typeof(Guid?) || info.PropertyType == typeof(Guid))
            {
                if (value != null)
                    info.SetValue(obj, new Guid(value), null);
            }

        }

        public static void FillControl(Control c, string v, object t)
        {
            if (null == c) return;

            if (c is PsPopWin)
            {
                PsPopWin pop = c as PsPopWin;
                pop.Text = (null == t) ? v : t.ToString();
                pop.Value = (null == t) ? null : v;
            }
            else if (c is TextBox)
            {
                TextBox txt = c as TextBox;
                txt.Text = v;
            }
            else if (c is DateTimePicker)
            {
                DateTimePicker dp = c as DateTimePicker;
                try
                {
                    if (dp.ShowCheckBox)
                    {
                        dp.Checked = !string.IsNullOrEmpty(v); //如果允许录入空，则复选框不选中
                    }

                    dp.Value = DateTime.Parse(v);
                }
                catch
                {
                    dp.Value = DateTime.Now;
                }
            }
            else if (c is PsSelect)
            {
                PsSelect sls = c as PsSelect;
                sls.Text = v;
            }
        }
    }
}
